package com.tuoren.forward.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.tuoren.forward.annotation.HandleLog;
import com.tuoren.forward.constant.CommonConstant;
import com.tuoren.forward.entity.req.DeviceAddReq;
import com.tuoren.forward.entity.req.DeviceRecordSearchReq;
import com.tuoren.forward.entity.req.DeviceSearchReq;
import com.tuoren.forward.entity.req.IdReq;
import com.tuoren.forward.entity.resp.DeviceResp;
import com.tuoren.forward.service.DeviceService;
import com.tuoren.forward.util.Result;
import com.tuoren.forward.util.ResultPage;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("device")
@Tag(name="设备接口")
public class DeviceController {
	
	@Autowired
	DeviceService deviceService;
	
	@PostMapping("search")
    @ResponseBody
    @Operation(summary = "查询设备")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultPage<DeviceResp> search(@RequestBody DeviceSearchReq req){
		log.info("search>>:{}",req);
		return deviceService.search(req);
	}
	

	@HandleLog
	@PostMapping("add")
    @ResponseBody
    @Operation(summary = "添加设备")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result add(@RequestBody DeviceAddReq req){
		log.info("add>>:{}",req);
		if(StringUtil.isEmpty(req.getProductId()) || StringUtil.isEmpty(req.getMac())) {
			return Result.fail(CommonConstant.LACK_PARAM);
		}
		return deviceService.add(req);
	}
	
	@HandleLog
	@PostMapping("edit")
    @ResponseBody
    @Operation(summary = "修改设备")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result edit(@RequestBody DeviceAddReq req){
		log.info("edit>>:{}",req);
		if(StringUtil.isEmpty(req.getId())) {
			return Result.fail(CommonConstant.LACK_PARAM);
		}
		return deviceService.edit(req);
	}
	
	@HandleLog
	@PostMapping("delete")
    @ResponseBody
    @Operation(summary = "删除设备")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result delete(@RequestBody IdReq req){
		log.info("delete>>:{}",req);
		if(StringUtil.isEmpty(req.getId())) {
			return Result.fail(CommonConstant.LACK_PARAM);
		}
		return deviceService.delete(req.getId());
	}
	
	@PostMapping("searchRecord")
    @ResponseBody
    @Operation(summary = "查询设备记录")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultPage<JSONObject> searchRecord(@RequestBody DeviceRecordSearchReq req){
		log.info("searchRecord>>:{}",req);
		return deviceService.searchRecord(req);
	}
	
}
